package cn.kgc.service.impl;

import cn.kgc.common.redisutils.stringRedisTemplateUtil;
import cn.kgc.dao.mapper.ItripHotelRoomMapper;
import cn.kgc.dao.mapper.ItripHotelTempStoreMapper;
import cn.kgc.pojo.ItripHotelRoom;
import cn.kgc.pojo.ItripUserLinkUser;
import cn.kgc.vo.Condition;
import cn.kgc.common.dtoutils.dtoUtil;
import cn.kgc.dao.mapper.ItripHotelOrderMapper;
import cn.kgc.dtos.Dto;
import cn.kgc.pojo.ItripHotelOrder;
import cn.kgc.service.ItripHotelOrderService;
import cn.kgc.common.fenye.PageUtil;
import cn.kgc.vo.ItripHotelOrderExtends;
import com.alibaba.fastjson.JSONObject;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service
public class ItripHotelOrderServiceimpl implements ItripHotelOrderService {

    @Resource
    private ItripHotelOrderMapper itripHotelOrderMapper;

    @Resource
    private stringRedisTemplateUtil stringRedisTemplateUtil;
    @Resource
    private ItripHotelRoomMapper itripHotelRoomMapper;
    @Resource
    private ItripHotelTempStoreMapper itripHotelTempStoreMapper;

    /**
     * 订单查询分页
     * @param condition 分页全部条件
     * @param token  个人id条件
     * @return list
     */
    @Override
    public Dto selectOrderAll(Condition condition,String token) {
        try {
            int index=1;
            if (condition.getPageNo()>0){
                index=condition.getPageNo();
            }
            ItripUserLinkUser itr= JSONObject.parseObject(stringRedisTemplateUtil.getKey(token),ItripUserLinkUser.class);
            PageUtil page=new PageUtil(itripHotelOrderMapper.selectCount(condition,(int)itr.getId()),index);
            page.setQueryData(condition);
            List<ItripHotelOrder> list=itripHotelOrderMapper.selectUserByQuery(page,(int)itr.getId());
            page.setRows(list);
           if (list.size()>0){
               return dtoUtil.getDto("0","获取个人订单列表成功", page,"true",null);
           }
           return dtoUtil.getDto("0002","没有订单",null,"false",null);
        }catch (Exception e){
            return dtoUtil.getDto("0003","查询异常",null,"false",e);
        }
    }

    /**
     * 订单详情
     * @param id  根据id查询
     * @return 对象
     */
    @Override
    public Dto selectOrderByid(int id) {
        try {
            ItripHotelOrderExtends itripHotelOrder = itripHotelOrderMapper.selectOrderByid(id);
            if (itripHotelOrder!=null){
                itripHotelOrder.setProcessNode((itripHotelOrder.getOrderStatus()+2)+"");
                if (itripHotelOrder.getOrderStatus()==2){
                    itripHotelOrder.setProcessNode(itripHotelOrder.getOrderStatus()+1+"");
                }
                return dtoUtil.getDto("0","查询成功", itripHotelOrder,"true",null);
            }else{
                ItripHotelRoom itripHotelRoom= itripHotelRoomMapper.selectByPrimaryKey((long)id);
                return dtoUtil.getDto("01","点评酒店查询", itripHotelRoom,"false",null);
            }
        }catch (Exception e){
            return dtoUtil.getDto("02","查询异常", null,"false",e);
        }
    }

    @Scheduled(cron="0/10 * *  * * ? ")
    @Override
    public void timer() {
        Date date = new Date();
        //获取所有订单待付款的信息
        List<ItripHotelOrder> list=itripHotelOrderMapper.selectAll();
        for(ItripHotelOrder itripHotelOrder:list){
            //获取当前时间减去订单创建时间差
            long time = date.getTime()- itripHotelOrder.getCreationDate().getTime();
            //获取小时
            //long hours = (time)/(1000* 60 * 60);
            //时间差转为分钟
            long minutes = (time)/(1000* 60);
            //判断订单是否超时
            if(1<=minutes){
                //执行修改订单状态
                int num1=itripHotelTempStoreMapper.updateById(itripHotelOrder.getId());
                if(num1>0){
                    //修改库存
                    int num=itripHotelTempStoreMapper.update(itripHotelOrder.getHotelId(),itripHotelOrder.getRoomId());
                }
            }
        }
    }
}
